package com.itheima.controller;

import com.aliyuncs.exceptions.ErrorMessageConstant;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.service.CheckItemService;
import com.itheima.pojo.CheckItem;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.PositiveOrZero;
import java.math.MathContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("checkitem")
public class CheckitemController {


    @Reference
    private CheckItemService checkItemService;

    //新增页面数据和更新编辑数据
    @RequestMapping("addUpdate.do")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.saveOrUpdate(checkItem);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_CHECKITEM_FAIL);
        }
    }


    //有条件和无条件都由该方法实现
    //分页查询数据
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {

            Map map = checkItemService.findPageByCondition(queryPageBean);



            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    //删除数据
    @RequestMapping("deleteCheckItemById.do")
    // @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    //添加这个注解对此方法进行权限限制，里面的值只能admin访问此方法
    public Result deleteCheckItemById(@RequestParam("id") Integer id){
        try {

            //获取对象
            CheckItem checkItem = new CheckItem();
            //设置选中当行的id删除之后把is_delet的值改成1
            checkItem.setId(id);
            checkItem.setIs_delete(1);

            checkItemService.updateById(checkItem);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    //  查询所有的检查项信息
    @RequestMapping("findAll")
    @PreAuthorize("hasRole('ADMIN')")
    public Result findAll(){
        try {
            List<CheckItem> list =  checkItemService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


    //  根据检查组的id  查询对应检查项ids
    @RequestMapping("findCheckItemIdsByGroupId.do")
    public Result findCheckItemIdsByGroupId(@RequestParam("id") int id){
        try {
            List<Integer> list =  checkItemService.findCheckItemIdsByGroupId(id);
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


}
